package com.shijiu.controller;


import com.shijiu.annotation.RepeatSubmit;
import com.shijiu.pojo.Category;
import com.shijiu.service.CateService;
import com.shijiu.utils.UserThreadLocal;
import com.shijiu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@RestController
@RequestMapping("cate")
@Transactional(rollbackFor = Exception.class)
public class CateController {

    @Autowired
    private CateService cateService;

    @ApiOperation("蛋糕列表")
    @PostMapping("catelist")
    public Result getCateList(){
        return Result.success(cateService.getCateList());
    }

    @ApiOperation("保存蛋糕")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("save")
    public Result saveCate(@RequestBody Category category){
        System.out.println(category);
        System.out.println(UserThreadLocal.get());
        category.setShopId((Integer) UserThreadLocal.get().get("shopId"));
        return Result.success(cateService.save(category));

    }


    @PostMapping("update")
    public Result updateCate(@RequestBody Category category){
        return Result.success(cateService.updateById(category));
    }

    @GetMapping("remove/{id}")
    public Result removeCate(@PathVariable("id") Integer id){
        return Result.success(cateService.removeById(id));
    }
}
